package com.arpa.wms.controller;

import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.ShipmentWarehouseValidate;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.consts.CommonConst;
import com.arpa.wms.domain.dto.InventoryRecordDTO;
import com.arpa.wms.domain.vo.ContainerVO;
import com.arpa.wms.domain.vo.InventoryRecordVO;
import com.arpa.wms.service.IInventoryRecordService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: Allen chen
 * @Date 2020/9/17
 * @Description:
 */

@Api(tags = "库存操作记录")
@RestController
@RequestMapping(value="/inventoryRecord", produces = "application/json;charset=UTF-8")
@Log4j2
public class InventoryRecordController {

    private final IInventoryRecordService iInventoryRecordService;

    public InventoryRecordController(IInventoryRecordService iInventoryRecordService) {
        this.iInventoryRecordService = iInventoryRecordService;
    }

    /**
     * 获取 库存操作记录 列表
     *
     * @param inventoryRecordDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 库存操作记录 列表", notes = "获取 库存操作记录 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ContainerVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(InventoryRecordDTO inventoryRecordDTO, HttpServletRequest request) {
        try {
            //校验授权仓库code和货主code不能为空
            Map<String, List<String>> authMap = ShipmentWarehouseValidate.validateShipmentCodesAndWarehouseCodes();
            // 去除只查询正式库存标识，用于查询所有库存
            //登录账号授权的仓库过滤
            inventoryRecordDTO.setGroupCode(UserUtil.getBranchCode());
            //登录账号授权的仓库过滤
            inventoryRecordDTO.setWarehouseCodes(authMap.get(CommonConst.WAREHOUSE_CODES));
            //登录账号授权的货主过滤
            inventoryRecordDTO.setShipmentCodes(authMap.get(CommonConst.SHIPMENT_CODES));

            InventoryRecordVO inventoryRecordVO = iInventoryRecordService.queryListSum(inventoryRecordDTO);
            List<InventoryRecordVO> inventoryRecordVoS = iInventoryRecordService.queryList(inventoryRecordDTO);
            Page returnPage = new Page(inventoryRecordDTO.getPageNum(),inventoryRecordDTO.getPageSize());
            returnPage.setRecords(inventoryRecordVoS);
            returnPage.setTotal(inventoryRecordVO.getTotal());
            returnPage.setSumObject(inventoryRecordVO);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }
}
